Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
file-entry-cache
Advanced tools
Super simple cache for file metadata, useful for process that work o a given series of files and that only need to repeat the job on the changed ones since the previous run of the process
The file-entry-cache npm package provides a simple way to cache the metadata of files in a directory. It is primarily used to speed up file processing tasks by caching the results of expensive file operations. This can be particularly useful in tasks like linting, where files that have not changed since the last run can be skipped. The package allows for creating, reading, and updating caches, and it can work with both flat file caches and more complex, structured data.
Create and manage a file cache
This feature allows you to create a new cache or load an existing one from disk. You can add or remove files from the cache and save the updated cache back to disk. This is useful for tracking which files have been processed and which have not.
const fileEntryCache = require('file-entry-cache');
const cache = fileEntryCache.create('.myCache');
// To add a file to the cache
cache.addEntry('path/to/file.js');
// To remove a file from the cache
cache.removeEntry('path/to/file.js');
// To save the cache to disk
cache.reconcile();
Check if a file has changed since last cached
This feature allows you to check if a file has been modified since it was last added to the cache. This is particularly useful for tools that perform operations on files and want to skip processing for files that have not changed.
const fileEntryCache = require('file-entry-cache');
const cache = fileEntryCache.create('.myCache');
// Assuming 'path/to/file.js' was previously added to the cache
const fileHasChanged = cache.hasFileChanged('path/to/file.js');
if (fileHasChanged) {
console.log('File has changed since last cache.');
} else {
console.log('File has not changed.');
}
Chokidar is a more comprehensive file watching library that provides a high-level API to watch files and directories for changes. It is similar to file-entry-cache in that it can be used to optimize file processing tasks by reacting to changes in real-time, but it operates on a different principle, focusing on event-based file watching rather than caching metadata.
node-persist is a local storage library for Node.js, allowing data to be stored and retrieved across sessions. While it is not specifically designed for caching file metadata, it can be used for similar purposes as file-entry-cache by manually managing file metadata as part of the stored data. The main difference is that node-persist offers a broader set of storage capabilities beyond just file metadata.
Super simple cache for file metadata, useful for process that work o a given series of files and that only need to repeat the job on the changed ones since the previous run of the process — Edit
npm i --save file-entry-cache
The module exposes two functions create
and createFromFile
.
create(cacheName, [directory, useCheckSum])
createFromFile(pathToCache, [useCheckSum])
// loads the cache, if one does not exists for the given
// Id a new one will be prepared to be created
var fileEntryCache = require('file-entry-cache');
var cache = fileEntryCache.create('testCache');
var files = expand('../fixtures/*.txt');
// the first time this method is called, will return all the files
var oFiles = cache.getUpdatedFiles(files);
// this will persist this to disk checking each file stats and
// updating the meta attributes `size` and `mtime`.
// custom fields could also be added to the meta object and will be persisted
// in order to retrieve them later
cache.reconcile();
// use this if you want the non visited file entries to be kept in the cache
// for more than one execution
//
// cache.reconcile( true /* noPrune */)
// on a second run
var cache2 = fileEntryCache.create('testCache');
// will return now only the files that were modified or none
// if no files were modified previous to the execution of this function
var oFiles = cache.getUpdatedFiles(files);
// if you want to prevent a file from being considered non modified
// something useful if a file failed some sort of validation
// you can then remove the entry from the cache doing
cache.removeEntry('path/to/file'); // path to file should be the same path of the file received on `getUpdatedFiles`
// that will effectively make the file to appear again as modified until the validation is passed. In that
// case you should not remove it from the cache
// if you need all the files, so you can determine what to do with the changed ones
// you can call
var oFiles = cache.normalizeEntries(files);
// oFiles will be an array of objects like the following
entry = {
key: 'some/name/file', the path to the file
changed: true, // if the file was changed since previous run
meta: {
size: 3242, // the size of the file
mtime: 231231231, // the modification time of the file
data: {} // some extra field stored for this file (useful to save the result of a transformation on the file
}
}
I needed a super simple and dumb in-memory cache with optional disk persistence (write-back cache) in order to make
a script that will beautify files with esformatter
to execute only on the files that were changed since the last run.
In doing so the process of beautifying files was reduced from several seconds to a small fraction of a second.
This module uses flat-cache a super simple key/value
cache storage with
optional file persistance.
The main idea is to read the files when the task begins, apply the transforms required, and if the process succeed,
then store the new state of the files. The next time this module request for getChangedFiles
will return only
the files that were modified. Making the process to end faster.
This module could also be used by processes that modify the files applying a transform, in that case the result of the
transform could be stored in the meta
field, of the entries. Anything added to the meta field will be persisted.
Those processes won't need to call getChangedFiles
they will instead call normalizeEntries
that will return the
entries with a changed
field that can be used to determine if the file was changed or not. If it was not changed
the transformed stored data could be used instead of actually applying the transformation, saving time in case of only
a few files changed.
In the worst case scenario all the files will be processed. In the best case scenario only a few of them will be processed.
stringify-able
ones if possible, flat-cache uses circular-json
to try to persist circular structures, but this should be considered experimental. The best results are always obtained with non circular valuesMIT
FAQs
A lightweight cache for file metadata, ideal for processes that work on a specific set of files and only need to reprocess files that have changed since the last run
The npm package file-entry-cache receives a total of 30,758,789 weekly downloads. As such, file-entry-cache popularity was classified as popular.
We found that file-entry-cache demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.